﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Security.Permissions;
using System.Security;

namespace P5.AttributeDemo
{
    public static class TypeHelper
    {
        public static object GetPropertyValue(object fromObject, string propertyName)
        {
            object propertyValue = null;

            foreach (PropertyInfo p in fromObject.GetType().GetProperties())
            {
                if (p.Name == propertyName)
                {
                    propertyValue = p.GetValue(fromObject, null);
                    break;
                }
            }

            return propertyValue;
        }
    }

    [AttributeUsage(AttributeTargets.Class)]
    public class HelpAttribute : Attribute
    {
        public string url;

        public string Url
        {
            get
            {
                return this.url;
            }
            set
            {
                this.url = value;
            }
        }

    }

    [Help(Url = "http://pentalog.vn")]
    public class AbstractFunction
    {
    }

    public class DenyAccessAttribute : CodeAccessSecurityAttribute
    {
        public DenyAccessAttribute()
            : base(SecurityAction.Deny)
        { }

        public override IPermission CreatePermission()
        {
            throw new NotImplementedException();
        }
    }

    [DenyAccess(Action = SecurityAction.Deny)]
    class Program
    {
        static void Main(string[] args)
        {
            Type info = typeof(AbstractFunction);
            foreach (object attribute in info.GetCustomAttributes(true))
            {
                foreach (PropertyInfo p in attribute.GetType().GetProperties())
                {
                    object po = p;
                    object v1 = TypeHelper.GetPropertyValue(p, "Url");
                    Console.WriteLine(v1);

                    object v = p.GetValue(attribute, null);
                    Console.WriteLine(v);
                    Console.WriteLine(p.ToString());
                }
                Console.WriteLine(attribute);
            }
            Console.ReadLine();
        }
    }
}
